package com.javaweb.dao.impl;

import com.javaweb.aop.ExceptionHandler;
import com.javaweb.dao.PermDao;
import com.javaweb.model.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@ExceptionHandler
@Repository
@Slf4j
public class PermDaoImpl implements PermDao {

    @Autowired
    private Connection conn ;
    @Override
    public Integer insert(Permission perm) {
        String sql = "INSERT INTO tb_permission (name, url) VALUES (?, ?)";
        try (PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setString(1, perm.getName());
            ps.setString(2, perm.getUrl());
            ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    // 插入角色和权限的关联
    @Override
    public Integer insertRolePerm(int roleId, int permId) {
        String sql = "INSERT INTO tb_role_per (role_id, per_id) VALUES (?, ?)";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setInt(1, roleId);
            ps.setInt(2, permId);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 根据名称和URL查询权限
    @Override
    public Permission selectOne(String name, String url) {
        String sql = "SELECT * FROM tb_permission WHERE name = ? AND url = ?";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setString(1, name);
            ps.setString(2, url);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    Permission perm = new Permission();
                    perm.setPerId(rs.getInt("id"));
                    perm.setName(rs.getString("name"));
                    perm.setUrl(rs.getString("url"));
                    return perm;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 根据ID查询权限
    @Override
    public Permission selectOneById(int perId) {
        String sql = "SELECT * FROM tb_permission WHERE per_id = ?";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setInt(1, perId);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    Permission perm = new Permission();
                    perm.setPerId(rs.getInt("per_id"));
                    perm.setName(rs.getString("name"));
                    perm.setUrl(rs.getString("url"));
                    return perm;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 根据角色名称查询权限列表
    @Override
    public List<Permission> selectPermsByRoleName(String roleName) {
        List<Permission> perms = new ArrayList<>();
        String sql = "SELECT p.* FROM tb_permission p " +
                "JOIN tb_role_per rp ON p.per_id = rp.per_id " +
                "JOIN tb_role r ON rp.role_id = r.role_id " +
                "WHERE r.name = ?";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setString(1, roleName);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    Permission perm = new Permission();
                    perm.setPerId(rs.getInt("id"));
                    perm.setName(rs.getString("name"));
                    perm.setUrl(rs.getString("url"));
                    perms.add(perm);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return perms;
    }

    // 根据角色ID查询权限列表
    @Override
    public List<Permission> selectPermsByRoleId(int roleId) {
        List<Permission> perms = new ArrayList<>();
        String sql = "SELECT p.* FROM tb_permission p " +
                "JOIN tb_role_per rp ON p.per_id = rp.per_id " +
                "WHERE rp.role_id = ?";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setInt(1, roleId);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    Permission perm = new Permission();
                    perm.setPerId(rs.getInt("id"));
                    perm.setName(rs.getString("name"));
                    perm.setUrl(rs.getString("url"));
                    perms.add(perm);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return perms;
    }

    // 根据ID删除权限
    @Override
    public Integer deleteById(int permId) {
        String sql = "DELETE FROM tb_permission WHERE per_id = ?";
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)) {
            ps.setInt(1, permId);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}